<?php
require_once(ROOT . LIBRARY .'/fields/common.inc');

class fields_tab extends fields_common
{
   public $fields = array();

   public function  __construct($data)
   {
      parent::__construct($data);
   }

   public function attachField(&$field)
   {
      $this->fields[$field->alias] = $field;
   }


   public function getFieldHTML($data)
   {
      if(!$this->fieldset) return '';

      
      $value = $data->{$this->alias}(null);
      $html = '';

      $html .= '<div><a href="javascript:AddInFieldset_'. $this->alias .'();" class="a_icon i_add">';
      $html .= gettext::translate('meccano', 'add') .'</a></div>';
      $html .= '<div id="fieldset_'. $this->alias .'">'.EOL;

      $i = 0;
      if(!empty($value))
      {
         foreach($value AS $v)
         {
            ++$i;
            $html .= '<div class="fieldset">'.EOL;
            $html .= '<input type="hidden" name="f[position]['.$this->alias.']['. $v->id .']" value="'. $v->position .'" rel="position" />'.EOL;
            $html .= '<table width="75%" cellpadding="0" cellspacing="0" border="0">'.EOL;

            reset($this->fields);
            foreach($this->fields AS $f)
            {
               if($f->active)
               {
                  $html .= '<tr><td class="rt">'. $f->title .':</td><td>'.EOL;
                  $html .= str_replace(array(EOL), array(''), $f->getFieldHTML($v));
                  $html .= '</td></tr>'.EOL;
               }
            }

            $html .= '</table>'.EOL;
            $html .= '<div class="manage">'.EOL;
            $html .= '<a class="moveable">'. gettext::translate('meccano', 'move') .'</a>'.EOL;
            $html .= '<a href="javascript:void(0)" onclick="DeleteInFieldset(this, \''. $this->alias .'\', '. $v->id;
            $html .= ');" class="a_icon i_delete">'. gettext::translate('meccano', 'delete') .'</a><br />'.EOL;
            $html .= '</div></div>'.EOL;
         }
      }

      $html .= '</div>'.EOL;

      $html .= '<script type="text/javascript">'.EOL;
      $html .= 'var '. $this->alias . '_id = 1;'.EOL;
      $html .= 'function AddInFieldset_'. $this->alias .'()'.EOL;
      $html .= '{'.EOL;
      $html .= 'var html = \'<div class="fieldset">\';'.EOL;
      $html .= 'html += \'<input type="hidden" name="f[position]['. $this->alias .'][-\'+'. $this->alias . '_id+\']" value="" rel="position" />\';'.EOL;
      $html .= 'html += \'<table width="75%" cellpadding="0" cellspacing="0" border="0">\';'.EOL;

      reset($this->fields);
      foreach($this->fields AS $f)
      {
         if($f->active)
         {
            //$f->fieldset = '-\'+'. $this->alias .'_id+\'';
            $f->fieldset = 'fieldset_marker';
            $html .= 'html += \'<tr><td class="rt">'. $f->title .':</td><td>\';'.EOL;
            $field_html =  str_replace(
                    array(EOL, 'fieldset_marker'),
                    array('', '-\'+'. $this->alias .'_id+\''),
                    addcslashes($f->getFieldHTML(NULL), "'"));
            $html .= 'html += \''. $field_html .'\';'.EOL;
            $html .= 'html += \'</td></tr>\';'.EOL;
            $f->fieldset = TRUE;
         }
      }

      $html .= 'html += \'</table>\';'.EOL;
      $html .= 'html += \'<div class="manage">\';'.EOL;
      $html .= 'html += \'<a class="moveable">'. gettext::translate('meccano', 'move') .'</a>\';'.EOL;
      $html .= 'html += \'<a href="javascript:void(0)" onclick="$(this).parent().parent().remove();" class="a_icon i_delete">'. gettext::translate('meccano', 'delete') .'</a><br />\';'.EOL;
      $html .= 'html += \'</div>\';'.EOL;
      $html .= '$(\'#fieldset_'. $this->alias .'\').prepend(html);'.EOL;
      $html .= $this->alias . '_id++;'.EOL;
      $html .= 'SwitchLang(current_lang);'.EOL;
      $html .= '}'.EOL;
      $html .= '$(\'#fieldset_'. $this->alias .'\').sortable({\'items\':\'>div\',\'handle\':\'.moveable\',\'revert\':true,\'update\':ResortFieldset});'.EOL;
      $html .= '</script>'.EOL;

      return $html;
   }
}
